<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <style>
        li {
            list-style: none;
        }
    </style>
</head>

<body>
    <div id="root">
        <button @click="student.age++">年龄+1</button>
        <button @click="addsex">添加性别默认男</button>
        <button @click="student.sex='女'">修改性别</button>
        <button @click="addfriend">添加一个朋友</button>
        <button @click="updataFirstFriendName">修改第一个朋友的名字</button>
        <button @click="addHobby">添加一个爱好</button>
        <button @click="updataHobby">修改第一个爱好为开车</button>
        <h1>学生信息</h1>
        <h2>学生姓名:{{student.name}}</h2>
        <h2>学生年龄：{{student.age}}</h2>
        <h2 v-if="student.sex">学生性别：{{student.sex}}</h2>
        <h2>朋友们</h2>

        <ul>
            <li v-for="(f,index) in student.friends" :key="index">
                {{f.name}} - {{f.age}}</li>
        </ul>
        <h2>爱好</h2>

        <ul>
            <li v-for="(h,index) in student.hobby" :key="index">
                {{h}} </li>
        </ul>


    </div>

    <script>

        // 用computed实现
        const vm = new Vue({
            el: '#root',
            data: {
                student: {
                    name: 'xiba',
                    sex: '',
                    age: '18',
                    hobby: ['吃瓜', '看书', '睡觉'],
                    friends: [
                        { name: 'goudan', age: '19' },
                        { name: 'ssb', age: '29' }
                    ]
                },



            },
            methods: {
                addsex() {
                    Vue.set(this.student, 'sex', '男')
                    // this.$set(this.student, 'sex', '男')
                },
                addfriend(){
                    // Vue.set(this.student.friends.unshift({name:'剑姬',age:'18'}))
                    this.student.friends.unshift({name:'剑姬',age:'18'})
                },
                updataFirstFriendName(){
                    // Vue.set(this.student.friends[0].name='刀妹')
                    this.student.friends[0].name='刀妹'

                },
                addHobby(){
                    this.student.hobby.push('学习')
                },
                updataHobby(){
                    this.student.hobby.splice(0,1,'开车')
                }
            },


        })
    </script>
</body>

</html>